<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>
<body>
	<div id="query_result">
		<h1>按月份统计交易收入情况</h1>
		<table id='tab' border="1">
			<tr>
				<th>月份</th>
				<th>收入（元）</th>
			</tr>
		</table>
	</div>

	<script type="text/javascript">
	//有重复记录的情况，有一些怪异情况
    var maps = '[{"value":"0.01","key":"2012-02"},{"value":"2432.21","key":"2012-03"},{"value":"0.00","key":"2012-01"},' 
   			          + '{"value":"22.21","key":"2011-03"},{"value":"2432.21","key":"2011-01"},{"value":"0.01","key":"2012-04"},' 
			          + '{"value":"0.01","key":"2012-05"},{"value":"0.02","key":"2012-02"}]';

	 (function(w){
		  // ie8 or ie9 console problem solution.	  
		  if(! w.console){
				//alert(".....");
				w.console = {
					info : function(){},
					error : function(){},
					log : function(){}
				};
		  }
	  })(window);

    
    window.onload = function(){
        var tab = document.getElementById('tab');
        if(maps){
            var jsonArr = eval(maps);
            
            var dateArr = [];
            for(var i = 0; i < jsonArr.length; i++){
                var obj = jsonArr[i];
                var key = obj.key;
                dateArr[i] = toDate(key);
                //console.info(dateArr[i].toUTCString() + "  " + dateArr[i].toLocaleString()  + "  " + dateArr[i].getTime());
            }

            console.info('----------------------------------');
            dateArr.sort(function(arg0, arg1){
                //return arg0 < arg1;
                // arg0.getTime() > arg1.getTime();
                return arg1.getTime() - arg0.getTime();  //desc order
            });
            
            for(var i = 0; i < dateArr.length; i++){
            	console.info(dateArr[i].toUTCString() + "  " + dateArr[i].toLocaleString()  + "  " + dateArr[i].getTime());
                var d = dateArr[i];
                var year = "" + (d.getYear() + 1900);
                var mon = d.getMonth();
                
                if(mon < 10){
                    mon = "0" + mon;
                }
                var keyTemp = year + '-' + mon;
                for(var j = 0; j < jsonArr.length; j++){
                    var obj = jsonArr[j];
                    var key = obj.key;
                    var val = obj.value;
                    
                    if(key == keyTemp){
                        //tab.rows[]
                        if(val != 0){
                            var row = document.createElement("tr");
                            tab.appendChild(row);
                            //var row = tab.insertRow(i +1);
                            row.insertCell(0).innerHTML = key;
                            row.insertCell(1).innerHTML = val;
                        }
                    }
                }
            }
            
        }
    }
    
    function toDate(yearMonth){
        var arr = yearMonth.split('-');
        var year = arr[0];
        var month = arr[1];
        
        return new Date(year,month,1);
    }
</script>
</body>
</html>